package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Product;
import cn.wolfcode.trip.base.domain.ProductOrder;
import cn.wolfcode.trip.base.domain.ProductSpecs;
import cn.wolfcode.trip.base.mapper.ProductMapper;
import cn.wolfcode.trip.base.mapper.ProductMapper;
import cn.wolfcode.trip.base.domain.ShoppingCart;
import cn.wolfcode.trip.base.mapper.ProductOrderMapper;
import cn.wolfcode.trip.base.mapper.ProductSpecsMapper;
import cn.wolfcode.trip.base.query.ProductOrderQuery;
import cn.wolfcode.trip.base.mapper.ShoppingCartMapper;
import cn.wolfcode.trip.base.service.IProductOrderService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Random;

/** tiger
 * 订单
 */
@Service
public class ProductOrderServiceImpl implements IProductOrderService {
    @Autowired
    private ProductOrderMapper productOrderMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductSpecsMapper productSpecsMapper;


    /** tiger
     * 新增订单
     * @param productOrder
     */
    public ProductOrder saveProductOrder(ProductOrder productOrder) {

        if(productOrder.getId()==null){
            //生成随机订单编号
            Random random = new Random();
            int num = random.nextInt();
            String str="lww"+num;
            //设置订单编号
            productOrder.setSequence(str);
            productOrderMapper.insert(productOrder);
            return productOrder;
        }else {
            //更新状态
            productOrder.setFreight(1);
            productOrderMapper.updateByfreight(productOrder);
            return null;
        }


    }

    /**
     * 查询某个订单详情
     * @param orderId
     * @return
     */
    @Override
    public ProductOrder selectByOrderId(Long orderId) {
        ProductOrder productOrder = productOrderMapper.selectByPrimaryKey(orderId);
        List<Product> products = productMapper.selectbyId(productOrder.getId());
        productOrder.setProduct(products);
        return productOrder;
    }

    /**
     * lituan
     * 查出订单内容
     * @param qo
     * @return
     */
    @Override
    public PageInfo<ProductOrder> queryProductOrder(ProductOrderQuery qo) {
        qo.setUserId(UserContext.getCurrentUser().getId());
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<ProductOrder> list = productOrderMapper.query(qo);
        for (ProductOrder productOrder : list) {
           List<Product> products =productMapper.selectByOrderId(productOrder.getId());
           productOrder.setProducts(products);
        }
        return new PageInfo<>(list);
    }

    @Override
    public void updateState(Long productOrderId, Integer freight) {
        productOrderMapper.updateState( productOrderId,freight);
    }

    @Override
    public void saveConnections(Map map) {
        productMapper.saveConnections(map);
    }
}
